﻿// copyright (c) 2025 author:fio66<fio66@foxmail.com>. All rights reserved.
// date: 2025-09-23 14:31:38
#ifndef _FFMPEG_WRAPPER_SRC_AUDIO_DEVICE_WIN32_H_
#define _FFMPEG_WRAPPER_SRC_AUDIO_DEVICE_WIN32_H_
#include <winrt/base.h>
#include <xaudio2.h>

#include <span>
#include <vector>

#include "src/ffmpeg_warpper_export.h"

namespace fio66 {
class AudioDeviceWin32 : public AudioDevice, public IXAudio2VoiceCallback {
 public:
  AudioDeviceWin32() = default;
  ~AudioDeviceWin32() final { Release(); };
  // AudioDevice
  bool Create(const AudioInfo& audio_info) final;
  void Release() final;
  void Start() final;
  void Stop() final;
  void SetData(std::span<uint8_t> data) final;
  
  // IXAudio2VoiceCallback
  void OnVoiceProcessingPassStart(UINT32 BytesRequired) final {}
  void OnVoiceProcessingPassEnd() final {}
  void OnStreamEnd() final{}
  void OnBufferStart(void* pBufferContext) final{}
  void OnBufferEnd(void* pBufferContext) final{
    Submit();
  }
  void OnLoopEnd(void* pBufferContext) final {}
  void OnVoiceError(void* pBufferContext, HRESULT Error) final{}

  private:
  bool Submit();

 private:
  uint32_t buffer_index_ = 0;
  std::vector<std::vector<uint8_t>> buffers_;
  winrt::com_ptr<IXAudio2> xaudio2_ = {};
  IXAudio2MasteringVoice* xaudio2_master_voice_ = nullptr;
  IXAudio2SourceVoice* xaudio2_source_voice_ = nullptr;
};
}  // namespace fio66

#endif  // _FFMPEG_WRAPPER_SRC_AUDIO_DEVICE_WIN32_H_
